using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Search;

namespace SE.HTMLRepeater.Components
{
    public class HTMLRepeaterController : ISearchable, IPortable
    {

        #region public method

        /// <summary>
        /// Gets all the HTMLRepeatersInfo objects for items matching the this moduleId
        /// </summary>
        /// <param name="moduleId"></param>
        /// <returns></returns>
        public List<HTMLRepeaterInfo> GetHTMLRepeaters(int moduleId)
        {
            return CBO.FillCollection<HTMLRepeaterInfo>(DataProvider.Instance().GetHTMLRepeaters(moduleId));
        }

        /// <summary>
        /// Get an info object from the database
        /// </summary>
        /// <param name="moduleId"></param>
        /// <param name="itemId"></param>
        /// <returns></returns>
        public HTMLRepeaterInfo GetHTMLRepeater(int moduleId, int itemId)
        {
            return (HTMLRepeaterInfo)CBO.FillObject(DataProvider.Instance().GetHTMLRepeater(moduleId, itemId), typeof(HTMLRepeaterInfo));
        }

        public int GetHTMLRepeatersCount(int @moduleId)
        {
            return DataProvider.Instance().GetHTMLRepeatersCount(moduleId);
        }



        /// <summary>
        /// Get a single page of items based on the page number and page size
        /// </summary>
        /// <param name="moduleId"></param>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <returns>Generic HTMLRepeaterInfo List</returns>
        public List<HTMLRepeaterInfo> GetHTMLRepeatersPaged(int moduleId,int pageNumber,int pageSize)
        {
            return CBO.FillCollection<HTMLRepeaterInfo>(DataProvider.Instance().GetHTMLRepeatersPaged(moduleId,pageNumber,pageSize));
        }
        



        /// <summary>
        /// Adds a new HTMLRepeatersInfo object into the database
        /// </summary>
        /// <param name="info"></param>
        public void AddHTMLRepeater(HTMLRepeaterInfo info)
        {
            //check we have some content to store
            if (info.Content != string.Empty)
            {
                DataProvider.Instance().AddHTMLRepeater(info.ModuleId, info.Content,info.DisplayOrder, info.CreatedByUser,info.AnchorText);
            }
        }

        /// <summary>
        /// update a info object already stored in the database
        /// </summary>
        /// <param name="info"></param>
        public void UpdateHTMLRepeater(HTMLRepeaterInfo info)
        {
            //check we have some content to update
            if (info.Content != string.Empty)
            {
                DataProvider.Instance().UpdateHTMLRepeater(info.ModuleId, info.ItemId, info.Content,info.DisplayOrder, info.CreatedByUser,info.AnchorText);
            }
        }


        /// <summary>
        /// Delete a given item from the database
        /// </summary>
        /// <param name="moduleId"></param>
        /// <param name="itemId"></param>
        public void DeleteHTMLRepeater(int moduleId, int itemId)
        {
            DataProvider.Instance().DeleteHTMLRepeater(moduleId, itemId);
        }

        /// <summary>
        /// Getsthe next order number for the specific module.
        /// </summary>
        /// <param name="moduleId"></param>
        /// <returns>int Next number in the order sequence</returns>
        public int GetNextOrder(int moduleId)
        {
            return DataProvider.Instance().GetNextHTMLRepeaterOrder(moduleId);
        }

        #endregion

        #region ISearchable Members

        /// <summary>
        /// Implements the search interface required to allow DNN to index/search the content of your
        /// module
        /// </summary>
        /// <param name="modInfo"></param>
        /// <returns></returns>
        public DotNetNuke.Services.Search.SearchItemInfoCollection GetSearchItems(ModuleInfo modInfo)
        {
            SearchItemInfoCollection searchItems = new SearchItemInfoCollection();

            List<HTMLRepeaterInfo> infos = GetHTMLRepeaters(modInfo.ModuleID);

            foreach (HTMLRepeaterInfo info in infos)
            {

                
                List<DisplayVar> Dvs = Util.DecompressVariables(info.Content);
                string ItemContent = info.Content;
                foreach (DisplayVar dv in Dvs)
                {
                    ItemContent = ItemContent.Replace("!" + dv.VarName + "!", dv.VarValue);
                }

                SearchItemInfo searchInfo = new SearchItemInfo(modInfo.ModuleTitle, ItemContent, info.CreatedByUser, info.CreatedDate,
                    modInfo.ModuleID, info.ItemId.ToString(), info.Content, "Item=" + info.ItemId.ToString());

                searchItems.Add(searchInfo);
            }

            return searchItems;
        }

        #endregion

        #region IPortable Members

        /// <summary>
        /// Exports a module to xml
        /// </summary>
        /// <param name="moduleID"></param>
        /// <returns></returns>
        public string ExportModule(int moduleID)
        {
            StringBuilder sb = new StringBuilder();

            List<HTMLRepeaterInfo> infos = GetHTMLRepeaters(moduleID);

            if (infos.Count > 0)
            {
                sb.Append("<HTMLRepeaters>");
                foreach (HTMLRepeaterInfo info in infos)
                {
                    sb.Append("<HTMLRepeater>");
                    sb.Append("<displayOrder>");
                    sb.Append(XmlUtils.XMLEncode(info.DisplayOrder.ToString()));
                    sb.Append("</displayOrder>");
                    sb.Append("<createdByUser>");
                    sb.Append(XmlUtils.XMLEncode(info.CreatedByUser.ToString()));
                    sb.Append("</createdByUser>");
                    sb.Append("<createdDate>");
                    sb.Append(XmlUtils.XMLEncode(info.CreatedDate.ToString()));
                    sb.Append("</createdDate>");
                    sb.Append("<content>");
                    sb.Append(XmlUtils.XMLEncode(info.Content));
                    sb.Append("</content>");
                    sb.Append("</HTMLRepeater>");
                }
                sb.Append("</HTMLRepeaters>");
            }

            return sb.ToString();
        }

        /// <summary>
        /// imports a module from an xml file
        /// </summary>
        /// <param name="ModuleID"></param>
        /// <param name="Content"></param>
        /// <param name="Version"></param>
        /// <param name="UserID"></param>
        public void ImportModule(int ModuleID, string Content, string Version, int UserID)
        {
            XmlNode infos = DotNetNuke.Common.Globals.GetContent(Content, "HTMLRepeaters");

            foreach (XmlNode info in infos.SelectNodes("HTMLRepeater"))
            {
                HTMLRepeater.Components.HTMLRepeaterInfo HTMLRepeaterInfo = new HTMLRepeaterInfo();
                HTMLRepeaterInfo.ModuleId = ModuleID;
                HTMLRepeaterInfo.Content = info.SelectSingleNode("./content").InnerText;
                HTMLRepeaterInfo.CreatedByUser = Convert.ToInt16(info.SelectSingleNode("./createdByUser").InnerText);
                HTMLRepeaterInfo.CreatedDate = Convert.ToDateTime(info.SelectSingleNode("./createdDate").InnerText);
                HTMLRepeaterInfo.DisplayOrder = Convert.ToInt16(info.SelectSingleNode("./displayOrder").InnerText);
                this.AddHTMLRepeater(HTMLRepeaterInfo);
            }
        }

        #endregion
    }
}
